﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace ChuyenDongVatLy
{
    public partial class frConlacdon : Form
    {
        public frConlacdon()
        {
            InitializeComponent();
        }

        public delegate void GetString(string chieudaiday,string goclech, string giatocg);
        public GetString GetMyValue;
        public delegate void GetString2(bool createfrom);
        public GetString2 GetMyValue2;

        private void btOpen_Click(object sender, EventArgs e)
        {
            if (txbChieudai.Text == "" || txbGiatocG.Text == "" || txbGoclech.Text == "")
            {
                MessageBox.Show("Ban phai dien day du thong tin!");
            }
            else
            {
                float g = float.Parse(txbGiatocG.Text);
                float fi = float.Parse(txbGoclech.Text);
                float l = float.Parse(txbChieudai.Text);
                if (g < 9 || g > 10 || l < 0 || l > 300 || l < 0 || l > 300 || fi < 0 || fi > 0.5)
                {
                    if (g < 9 || g > 10)
                        MessageBox.Show("Gia tốc phải trong khoảng từ 9->10");


                    if (fi < 0 || fi > 0.5)
                        MessageBox.Show("Góc fi không phù hợp 0->0.5pi");

                    if (l < 100 || l > 300)
                        MessageBox.Show("chiều dài dây trong khoảng 100->300cm");
                    
                }
                else
                {
                    GetMyValue(txbChieudai.Text, txbGoclech.Text, txbGiatocG.Text);
                }                     
            }
            
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (txbGiatocG.Text == "" || txbGoclech.Text == "" || txbChieudai.Text == "")
                MessageBox.Show("Cần điền đầy đủ thông sô !!!");
            else
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.Title = "Save as";
                saveFile.Filter = "*.txt|";
                if (saveFile.ShowDialog() == DialogResult.OK)
                {
                    FileStream f = new FileStream(saveFile.FileName.ToString() + ".txt", FileMode.Create);
                    StreamWriter srw = new StreamWriter(f);
                    srw.Write(txbChieudai.Text);
                    srw.WriteLine();
                    srw.Write(txbGiatocG.Text);
                    srw.WriteLine();
                    srw.Write(txbGoclech.Text);
                    srw.WriteLine();                    
                    srw.Close();
                    f.Close();
                }
            }
           
        }

        private void btLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFile.RestoreDirectory = true;
            DialogResult result = openFile.ShowDialog();
            if (result == DialogResult.OK)
            {
                string filename = openFile.FileName;
                string[] lines = File.ReadAllLines(filename);
                txbChieudai.Text = lines[0].ToString();
                txbGiatocG.Text = lines[1].ToString();
                txbGoclech.Text = lines[2].ToString();
            }
            
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            if (GetMyValue2 != null)
            {
                GetMyValue2(true);
            }
            this.Close();
        }

        private void txbChieudai_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && txbChieudai.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }

        private void txbTanso_KeyPress(object sender, KeyPressEventArgs e)
        {
            //string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            //char decimalChar = Convert.ToChar(decimalString);

            //if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            //else if (e.KeyChar == decimalChar && txbTanso.Text.IndexOf(decimalString) == -1)
            //{ }
            //else
            //{
            //    e.Handled = true;
            //}
        }

        private void txbGoclech_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && txbGoclech.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }

        private void txbGiatocG_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && txbGiatocG.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }
    }
}
